package com.tusuo.stds.ui.complete;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.tusuo.stds.R;
import com.tusuo.stds.rest.CompleteResult;
import com.tusuo.stds.rest.LevelRecordDetail;
import com.tusuo.stds.ui.BaseToolbarActivity;
import com.tusuo.stds.ui.ToolbarOwner;
import com.tusuo.stds.ui.question.LevelRecordDetailActivity;
import com.tusuo.stds.ui.question.LevelRecordDetailActivity_;
import com.tusuo.stds.ui.similarity.SimilarityTempActivity_;
import com.tusuo.stds.util.Utils;

import org.androidannotations.annotations.AfterInject;
import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.res.StringRes;
import org.parceler.Parcels;

import java.util.ArrayList;
import java.util.List;

import at.grabner.circleprogress.CircleProgressView;
import at.grabner.circleprogress.UnitPosition;

/**
 * Created by developer on 17-9-28.
 */

@EActivity(R.layout.activity_complete_result)
public class CompleteResultActivity extends BaseToolbarActivity<CompleteResultView,CompleteResultPresenter> implements CompleteResultView{

    @ViewById(R.id.complete_result_tv_title)
    TextView tvTitle;

    @ViewById(R.id.complete_result_circleview)
    CircleProgressView circleProgressView;
    @ViewById(R.id.complete_result_tv_clearance_time)
    TextView tvClearanceTime;
    @ViewById(R.id.complete_result_tv_persent)
    TextView tvPersent;

    @ViewById(R.id.complete_result_layout_back)
    LinearLayout layoutOnlyBack;
    @ViewById(R.id.complete_result_layout_error)
    RelativeLayout layoutError;
    @ViewById(R.id.complete_result_layout_main)
    LinearLayout layoutMain;

    @StringRes(R.string.toolbar_title_similarity_result)
    String toolbarTitle;
    @StringRes(R.string.err_msg_server)
    String strErrMsgServer;


    private CompleteResultPresenter completeResultPresenter;
    private int mId;
    private CompleteResult mCompleteResult;

    @AfterInject
    void initDI() {
        Intent intent = getIntent();
        if (intent != null && intent.getExtras() != null) {
            mId = intent.getIntExtra("id", -1);
        }
    }

    @AfterViews
    void initData() {
        setupUI(this);
        List<ToolbarOwner.MenuAction> menuActionList = new ArrayList<>();
        ToolbarOwner.Config config = new ToolbarOwner.Config(toolbarTitle, true, menuActionList);
        mToolbarOwner.setConfig(config);
        layoutError.setVisibility(View.VISIBLE);
        layoutMain.setVisibility(View.GONE);
        completeResultPresenter.loadData(mId);
    }

    @NonNull
    @Override
    public CompleteResultPresenter createPresenter() {
        completeResultPresenter = new CompleteResultPresenter();
        return completeResultPresenter;
    }

    @Override
    public void initView(CompleteResult completeResult) {
        mCompleteResult = completeResult;
        tvTitle.setText(completeResult.Title);
        circleProgressView.setValue(completeResult.CorrectRate * 100);
        tvPersent.setText((int)(completeResult.CorrectRate * 100) + "");
        circleProgressView.setUnitPosition(UnitPosition.RIGHT_BOTTOM);
        tvClearanceTime.setText(getString(R.string.level_clearance_time, Utils.secondsToHumanReadable(completeResult.ClearanceTime)));
        layoutError.setVisibility(View.GONE);
        layoutMain.setVisibility(View.VISIBLE);

    }

    @Click(R.id.complete_result_btn_back)
    void clickBackBtn() {
        if (Utils.UI.isFastDoubleClick()) {
            return;
        }
        finish();
    }


    @Click(R.id.complete_result_layout_details)
    void clickRecordDetailsBtn() {
        if (Utils.UI.isFastDoubleClick()) {
            return;
        }
        Intent intent = new Intent(this,LevelRecordDetailActivity_.class);
        LevelRecordDetail detail = new LevelRecordDetail();
        detail.Title = mCompleteResult.Title + "解析";
        detail.QuestionList = mCompleteResult.QuestionList;
        detail.QuestionCount = mCompleteResult.QuestionCount;
        intent.putExtra(LevelRecordDetail.NAME, Parcels.wrap(detail));
        intent.putExtra("isLevel",false);
        startActivity(intent);
    }

}
